package com.oro.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oro.dao.StudentDao;
import com.oro.domain.Banca;
import com.oro.domain.BankBranch;
import com.oro.domain.Categoria;
import com.oro.domain.Contatto;
import com.oro.domain.Corso;
import com.oro.domain.IndirizzoIT;
import com.oro.domain.Iscrizione;
import com.oro.domain.MacroCategoria;
import com.oro.domain.MigrationAgent;
import com.oro.domain.Scuola;
import com.oro.domain.Sede;
import com.oro.domain.Studente;
import com.oro.domain.Traduttore;
import com.oro.service.StudentService;

public class StudentServiceImpl implements StudentService {

	@Autowired
	StudentDao studentDao;

	@Override
	public void delete(Contatto c) {
		studentDao.delete(c);

	}

	@Override
	public void delete(Corso course) {
		studentDao.delete(course);
	}

	@Override
	public void delete(Iscrizione enr) {
		studentDao.delete(enr);

	}

	@Override
	public void delete(Scuola school) {
		studentDao.delete(school);

	}

	@Override
	public void delete(Studente stud) {
		studentDao.delete(stud);
	}

	@Override
	public void deleteBranches() {
		studentDao.deleteBranches();

	}

	@Override
	public void deleteContact(Long id) {
		studentDao.deleteContact(id);

	}

	@Override
	public void deleteCourse(Long id) {
		studentDao.deleteCourse(id);
	}

	@Override
	public void deleteCourses() {
		studentDao.deleteCourses();

	}

	@Override
	public void deleteIscrizione(Long id) {
		studentDao.deleteEnrollment(id);

	}

	@Override
	public void deleteSchool(Long id) {
		studentDao.delete(this.getScuola(id));

	}

	@Override
	public void deleteSchools() {
		studentDao.deleteSchools();

	}

	@Override
	public void deleteStudent(Long id) {
		studentDao.deleteStudent(id);

	}

	@Override
	public Sede getBranch(Long id) {
		return studentDao.getBranch(id);
	}

	@Override
	public List<Sede> getBranches() {
		return studentDao.getBranches();
	}

	@Override
	public List<Categoria> getCategories() {
		return studentDao.getCategories();
	}

	@Override
	public List<Categoria> getCategoriesByMacroCategoryId(Long id) {
		return studentDao.getCategoriesByMacroCategoryId(id);
	}

	@Override
	public List<Categoria> getCategoriesBySchool(Long schoolId) {
		Scuola school = studentDao.getSchool(schoolId);
		List<Corso> corsi = studentDao.getCoursesBySchool(school);
		List<Categoria> res = new ArrayList<Categoria>();
		Categoria cat;
		for (Corso c : corsi) {
			cat = c.getCategoria();
			if (!res.contains(cat)) {
				res.add(cat);
			}
		}
		return res;
	}

	@Override
	public Categoria getCategory(Long id) {
		return studentDao.getCategory(id);
	}

	@Override
	public List<Contatto> getContacts() {
		return studentDao.getContacts();
	}

	@Override
	public List<Contatto> getContactsByBranch(Sede sede) {
		return studentDao.getContactsByBranch(sede);
	}

	@Override
	public List<Contatto> getContactsByName(String name) {
		return studentDao.getContactsByName(name);
	}

	@Override
	public List<Contatto> getContactsByPhone(String number) {
		return studentDao.getContactsByPhone(number);
	}

	@Override
	public List<Contatto> getContactsBySchool(Scuola s) {
		return studentDao.getContactsBySchool(s);
	}

	@Override
	public List<Contatto> getContactsByStudent(Studente stud) {
		return studentDao.getContactsByStudent(stud);
	}

	@Override
	public List<Contatto> getContactsBySurname(String surname) {
		return studentDao.getContactsBySurname(surname);
	}

	@Override
	public Contatto getContatto(Long id) {
		return studentDao.getContact(id);
	}

	@Override
	public Corso getCorso(Long id) {
		return studentDao.getCorso(id);
	}

	@Override
	public List<Corso> getCourses() {
		return studentDao.getCourses();
	}

	@Override
	public List<Corso> getCoursesByBranch(Sede s) {
		return studentDao.getCoursesByBranch(s);
	}

	@Override
	public List<Corso> getCoursesByCategory(String category) {
		return studentDao.getCoursesByCategory(category);
	}

	@Override
	public List<Corso> getCoursesByMacroCategory(String macroCategory) {
		return studentDao.getCoursesByMacroCategory(macroCategory);
	}

	@Override
	public List<Corso> getCoursesByName(String name) {
		return studentDao.getCourseByName(name);
	}

	@Override
	public List<Corso> getCoursesBySchool(Scuola s) {
		return studentDao.getCoursesBySchool(s);
	}

	@Override
	public List<Corso> getCoursesByState(String state) {
		return studentDao.getCoursesByState(state);
	}

	@Override
	public List<Corso> getCoursesByTown(String town) {
		return studentDao.getCoursesByTown(town);
	}

	@Override
	public Iscrizione getEnrollment(Long id) {
		return studentDao.getEnrollment(id);
	}

	@Override
	public List<Iscrizione> getEnrollments() {
		return studentDao.getEnrollments();
	}

	@Override
	public List<Iscrizione> getEnrollmentsByBranch(Sede s) {
		return studentDao.getEnrollmentsByBranch(s);
	}

	@Override
	public List<Iscrizione> getEnrollmentsByCourse(Corso c) {
		return studentDao.getEnrollmentsByCourse(c);
	}

	@Override
	public List<Iscrizione> getEnrollmentsBySchool(Scuola s) {
		return studentDao.getEnrollmentsBySchool(s);
	}

	@Override
	public List<Iscrizione> getEnrollmentsByStudent(Studente stud) {
		return studentDao.getEnrollmentsByStudent(stud);
	}

	@Override
	public List<MacroCategoria> getMacroCategories() {
		return studentDao.getMacroCategories();
	}

	@Override
	public Scuola getSchool(Long id) {
		return studentDao.getSchool(id);
	}

	@Override
	public Scuola getSchool(Long id, boolean eager) {
		if (eager = false)
			return getSchool(id);
		else {
			Scuola school = getSchool(id);
			refresh(school);
			for (Sede s : school.getBranches()) {
				refresh(s);
				for (Corso c : s.getCorsi()) {
					refresh(c);
				}
			}
			return school;
		}
	}

	@Override
	public List<Scuola> getSchools() {
		return studentDao.getSchools();
	}

	@Override
	public List<Scuola> getSchoolsByCourseCategory(Long id) {
		return studentDao.getSchoolsByCourseCategory(id);
	}

	@Override
	public List<Scuola> getSchoolsByCourseCategory(String category) {
		return studentDao.getSchoolsByCourseCategory(category);
	}

	@Override
	public List<Scuola> getSchoolsByCourseMacroCategory(String macroCategory) {
		return studentDao.getSchoolsByCourseMacroCategory(macroCategory);
	}

	@Override
	public List<Scuola> getSchoolsByCoursePrice(float min, float max) {
		return studentDao.getSchoolsByCoursePrice(min, max);
	}

	@Override
	public List<Scuola> getSchoolsByName(String name) {
		return studentDao.getSchoolsByName(name);
	}

	@Override
	public List<Scuola> getSchoolsByState(String state) {
		return studentDao.getSchoolsByState(state);
	}

	@Override
	public List<Scuola> getSchoolsByTown(String town) {
		return studentDao.getSchoolsByTown(town);
	}

	@Override
	public List<String> getSchoolsNames() {
		return studentDao.getSchoolsNames();
	}

	@Override
	public Scuola getScuola(Long id) {
		return studentDao.getSchool(id);
	}

	@Override
	public Studente getStudent(Long id) {
		return studentDao.getStudent(id);
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	@Override
	public List<Studente> getStudentiByName(String firstName) {
		return studentDao.getStudentiByName(firstName);
	}

	@Override
	public List<Studente> getStudentiByPhone(String number) {
		return studentDao.getStudentiByPhone(number);
	}

	@Override
	public List<Studente> getStudentiBySchool(Scuola school) {
		List<Iscrizione> iscrizioniScuola = this.getEnrollmentsBySchool(school);
		List<Studente> res = new ArrayList<Studente>();
		for (Iscrizione i : iscrizioniScuola) {
			res.add(i.getStudente());
		}
		return res;
	}

	@Override
	public List<Studente> getStudentiByState(String state) {
		return studentDao.getStudentiByState(state);
	}

	@Override
	public List<Studente> getStudentiBySurname(String surname) {
		return studentDao.getStudentiBySurname(surname);
	}

	@Override
	public List<Studente> getStudentiByTown(String town) {
		return studentDao.getStudentiByTown(town);
	}

	@Override
	public List<Studente> getStudents() {
		return studentDao.getStudents();
	}

	@Override
	public List<String> getStudentsEmails() {
		return studentDao.getStudentsEmails();
	}

	@Override
	public List<String> getStudentsEmailsByTown(String studentTown) {
		return studentDao.getStudentsEmailsByTown(studentTown);
	}

	@Override
	public List<String> getStudentsNames() {
		return studentDao.getStudentsNames();
	}

	@Override
	public List<String> getStudentsNamesByTown(String studentTown) {
		return studentDao.getStudentsNamesByTown(studentTown);
	}

	@Override
	public List<String> getStudentsSurnames() {
		return studentDao.getStudentsSurnames();
	}

	@Override
	public List<String> getStudentsSurnamesByTown(String studentTown) {
		return studentDao.getStudentsSurnamesByTown(studentTown);
	}

	@Override
	public List<String> getStudentTowns() {
		List<Studente> students = studentDao.getStudents();
		List<String> res = new ArrayList<String>();
		List<IndirizzoIT> inds;
		for (Studente s : students) {
			inds = s.getIndirizziIt();
			for (IndirizzoIT ind : inds) {
				if (ind != null) {
					if (!res.contains(ind.getCitta()) && ind.getCitta() != null
							&& ind.getCitta() != "")
						res.add(ind.getCitta());
				}
			}
		}
		return res;
	}

	@Override
	public List<String> getTownsAu() {
		return studentDao.getTownsAu();
	}

	@Override
	public List<String> getTownsIt() {
		return studentDao.getTownsIt();
	}

	@Override
	public void refresh(Object o) {
		studentDao.refresh(o);

	}

	@Override
	public void save(Corso corso) {
		studentDao.save(corso);

	}

	@Override
	public void save(Scuola school) {
		studentDao.save(school);
	}

	@Override
	public void save(Sede branch) {
		studentDao.save(branch);
	}

	@Override
	public void save(Studente student) {
		studentDao.save(student);
	}

	@Override
	public List<Scuola> searchSchools(String schoolName, String schoolTown,
			Long courseCategoryId, float minPrice, float maxPrice) {
		return studentDao.searchSchools(schoolName, schoolTown,
				courseCategoryId, minPrice, maxPrice);
	}

	@Override
	public List<Studente> searchStudents(String firstName, String surname,
			String email, String studentTown, Long schoolId,
			Long courseCategoryId) {
		List<Studente> result = studentDao.searchStudents(firstName, surname,
				email, studentTown, schoolId, courseCategoryId);
		return result;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void save(MigrationAgent migrationAgentInput) {
		studentDao.save(migrationAgentInput);

	}

	@Override
	public void save(Traduttore translatorInput) {
		studentDao.save(translatorInput);

	}

	@Override
	public void initialize(Object o) {
		studentDao.initialize(o);

	}

	@Override
	public void update(Object o) {
		studentDao.update(o);

	}

	/**
	 * Initializes the collection and objects, so that the collection can be
	 * displayed and modified.
	 */
	@Override
	public void prepare(Studente studentInput) {
		studentDao.prepare(studentInput);
	}

	@Override
	public void prepare(Scuola school) {
		studentDao.prepare(school);
		
	}

	@Override
	public Banca getBank(Long id) {
		return studentDao.getBank(id);
	}

	@Override
	public Banca getBankByName(String name) {
		return studentDao.getBankByName(name);
	}

	@Override
	public List<String> getBankNames() {
		return studentDao.getBankNames();
	}

	@Override
	public List<Banca> getBanks() {
		return studentDao.getBanks();
	}

	@Override
	public void save(Banca bank) {
		studentDao.save(bank);
		
	}

	@Override
	public void save(BankBranch bankBranch) {
		studentDao.save(bankBranch);
		
	}

	@Override
	public List<BankBranch> getBankBranches(Banca bank) {
		return studentDao.getBankBranches(bank);
		
	}

	@Override
	public BankBranch getBankBranch(Long id) {
		return studentDao.getBankBranch(id);
	}
}
